<script type="text/javascript">
	function TermApplyCtrl(CtrlUtil, $http, $element, $injector, $q, modalService, $scope, toaster) {
		var ctrl = this;
		ctrl.pageNumber = 0;
		ctrl.qs = -1;
		ctrl.statusQueryParam = $scope.$user.type
		ctrl.onQuerySuccess = function (r) {
			return $http.post(':root/term/getApplyCount', {
				status: 3
			}).success(function (R) {
				ctrl.passed = R && R[0] && R[0][1]
			})
		}
		CtrlUtil.build(ctrl, ':root/term/queryTermStuApply')
		CtrlUtil.loadDepartment(ctrl)
		this.changeDepartment = function () {
			CtrlUtil.loadMajor(ctrl, ctrl.departmentQueryParam)
		}
		CtrlUtil.loadMajor(ctrl)
		this.majorLoad = function (data) {
			ctrl.majorDef = {}
			angular.forEach(data, function (v) {
				ctrl.majorDef[v.id] = v
			})
		}
		this.dialogPop = function (item, option, viewMode) {
			var q = $q.defer();
			var content = $element.find('.tpl>form').clone()
			var method = 1 ? 'confirm' : 'show'
			modalService[method]({
				title: '审核',
				content: content,
				result: item,
				viewMode: viewMode,
				ctrl: this
			}, option).then(function (r) {
				q.resolve(r);
			})
			return q.promise;
		}
		this.audit = function (item) {
			item.pass = true
			ctrl.dialogPop(item, {
				backdrop: false
			}).then(
				function (r) {
					$http.post(':root/term/examineTermStuApply', {
						id: item.id,
						action: item.pass ? 1 : 0
					}).success(function (r) {
						$injector.get('modalService').succ('审核成功', 1500)
						ctrl.query()
					})

				})
		}
		this.view = function (item) {
			ctrl.dialogPop(item, {
				backdrop: false
			}, true)
		}
		this.update = function (olditem) {
			var item = {}
			angular.copy(olditem, item)
			if (item.status == 3) {
				toaster.error('不能修改已审核通过的数据')
				return;
			}
			modalService.confirm({
				title: '修改数据',
				contentUrl: ':root/components/common/termApplyForm.html',
				result: item,
				size: 'lg',
				ctrl: this
			}).then(function (R) {
				if (item.status == 3) {
					toaster.error('不能修改为已审核通过的数据')
					return;
				}
				modalService.confirm({
					title: '修改确认',
					content: '确定要修改本条申请数据吗?'
				}).then(function (r) {
					$http.post(':root/term/updateTermStuApply', R).success(function (re) {
						if (re) {
							toaster.success('修改成功')
							ctrl.query()
						}
					})
				})
			})
		}
		this.batch = function (pass) {
			var checked = CtrlUtil.getChecked(ctrl)
			if (checked.length == 0) {
				return;
			}
			var ids = [];
			angular.forEach(checked, function (v) {
				ids.push(v.id);
			})
			var content = '确定要' + (pass ? '<label class="text-success">审核通过</label>' : '<label class="text-danger">批量驳回</label>') + '选中的'
				+ ids.length + '条数据吗?'
			return modalService.confirm({
				title: '审核',
				content: content
			}).then(function (r) {
				$http.post(':root/term/examineTermStuApplies', {
					ids: ids,
					action: pass ? 1 : 0
				}).success(function (r) {
					modalService.succ('批量审核成功', 1500)
					ctrl.query()
				})

			});
		}
	}

</script>
<!-- Search area -->
<div ng-controller="TermApplyCtrl as ctrl" ng-init="ctrl.query()">
	<data entity="Major" auto-load="true" after-refresh="ctrl.majorLoad(data)" page="false"></data>
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span> 信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal row">
					<data entity="TrainType" bind-ctrl="ctrl.type" auto-load="true" page="false"></data>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.trainTypeQueryParam" ng-options="t.id as t.name for t in ctrl.type.getData()" class="form-control">
								<option value="">培训类别</option>
							</select>

						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="学号/姓名" ng-model="ctrl.xhQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select class="form-control" chosen ng-change="ctrl.changeDepartment()" ng-options="m.id as m.name for m in ctrl.departments" ng-model="ctrl.departmentQueryParam">
								<option value="">院系</option>
							</select>

						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select class="form-control" chosen ng-options="m.id as m.name for m in ctrl.majors" ng-model="ctrl.majorQueryParam">
								<option value="">专业</option>
							</select>
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="年级" ng-model="ctrl.gradeQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.statusQueryParam" class="form-control">
								<option ng-value="">审核状态</option>
								<option ng-if="$user.type==1" ng-value="1">待审核</option>
								<option ng-if="$user.type>1" ng-value="2">待审核</option>
								<option ng-if="$user.type==1" ng-value="2">待学校系审核</option>
								<option ng-value="3">审核通过</option>
								<option ng-if="$user.type>1" ng-value="-1">请求驳回</option>
							</select>
						</div>
					</div>
					<div class="form-group col-md-12 remove-margin-horizontal ">
						<div class="col-md-10 col-md-offset-1  pull-right">
							<div class="pull-right ">
								<button ng-click="ctrl.query()" type="submit" class="btn btn-cys btn-sm">
									<span class="glyphicon glyphicon-search"></span>
									查询
								</button>
								<button ng-click="ctrl.reset()" type="reset" class="btn btn-default btn-sm">
									<span class="glyphicon glyphicon-repeat"></span>
									重置
								</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<!-- Search area END -->

	<!-- operation area -->
	<button type="button" class="btn btn-cys btn-sm" ng-click="ctrl.batch(true)">
		<span class="glyphicon glyphicon-ok"></span>
		批量通过
	</button>
	<button type="button" class="btn btn-danger btn-sm" ng-click="ctrl.batch(false)">
		<span class="glyphicon glyphicon-remove"></span>
		批量驳回
	</button>
	<a type="button" class="btn btn-cys btn-sm" ng-click="export(ctrl,'root/term/export')">
		<span class="glyphicon glyphicon-export"></span>
		导出
	</a>
	<div class="ny-top-r pull-right" ng-include="':root/components/page.html'"></div>
	<!-- operation area END -->

	<!-- 期次新增、编辑弹出框 -->



	<!-- data grid area -->
	<div style="padding-top: 10px;">
		<table class="table table-bordered table-hover table-striped text-center">
			<tr>
				<th>
					<input ng-change="ctrl.checkAll(ctrl.$checkAll)" ng-model="ctrl.$checkAll" type="checkbox" />
				</th>
				<th>姓名</th>
				<th>性别</th>
				<th>学号</th>
				<th>年级</th>
				<th>院系</th>
				<th>专业</th>
				<th>学生类别</th>
				<th>培训类别</th>
				<th>提交时间</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td>
					<input ng-model="item.$checked" type="checkbox" />
				</td>
				<td>{{item.user.name}}</td>
				<td>{{item.sexString}}</td>
				<td>{{item.user.uid}}</td>
				<td>{{item.user.grade}}</td>
				<td>{{ctrl.departmentMap[item.user.department].name}}</td>
				<td>{{ctrl.majorDef[item.user.major].name}}</td>
				<td>{{item.user.teacher?'教师':'学生'}}</td>
				<td>{{item.term.trainType.name}}</td>
				<td>{{item.applyTime|date:'yyyy-MM-dd HH:mm'}}</td>
				<td ng-switch="item.status">
					<span ng-switch-when="1">待院系审核</span>
					<span ng-switch-when="2">待学校审核</span>
					<span ng-switch-when="3">审核通过</span>
				</td>
				<td>
					<a ng-hide="item.status==3||(item.status>$user.type)" href="javascript:;" class="remove-underline" ng-click="ctrl.audit(item)">审核</a>
					<a href="javascript:;" class="remove-underline" ng-click="ctrl.view(item)">查看</a>
					<a ng-show="item.status!=3&&$user.type>1" href="javascript:;" class="remove-underline" ng-click="ctrl.update(item)">修改</a>
				</td>
			</tr>
		</table>
		<p>
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span> 条
		</p>
	</div>
	<!-- data grid area END -->


	<div ng-non-bindable class="tpl hidden">

		<form class="form-horizontal table-responsive">
			<table class="table  table-bordered">
				<tr>
					<td colspan="4" class="text-center">
						<label>报名期次：</label> {{result.term.trainType.name+''+result.term.name}}
					</td>
				</tr>
				<tr>
					<td class="text-right">
						<label>姓名：</label>
					</td>
					<td>{{result.user.name}}</td>
					<td class="text-right">
						<label>性别：</label>
					</td>
					<td>{{result.sexString}}</td>
				</tr>
				<tr>
					<td class="text-right">
						<label>学号：</label>
					</td>
					<td>{{result.user.uid}}</td>
					<td class="text-right">
						<label>年级：</label>
					</td>
					<td>{{result.user.grade}}</td>

				</tr>
				<tr>
					<td class="text-right">
						<label>学生类别：</label>
					</td>
					<td>{{result.user.type}}</td>
					<td class="text-right">
						<label>所在校区：</label>
					</td>
					<td>{{result.xq}}</td>

				</tr>
				<tr>
					<td class="text-right">
						<label>联系方式：</label>
					</td>
					<td colspan="1">{{result.phone}}</td>
					<td class="text-right">
						<label>QQ：</label>
					</td>
					<td colspan="1">{{result.qq}}</td>
				</tr>
				<tr>
					<td class="text-right">
						<label>职务：</label>
					</td>
					<td colspan="1">{{result.zw}}</td>
					<td class="text-right">
						<label>院系：</label>
					</td>
					<td colspan="1">{{ctrl.departmentMap[result.user.department].name}}</td>
				</tr>
				<tr>
					<td class="text-right">
						<label>专业：</label>
					</td>
					<td colspan="3">{{ctrl.majorDef[result.user.major].name}}</td>
				</tr>
				<tr ng-hide="viewMode">
					<td colspan="4" class="text-center">
						<label>审批意见：</label>
						<input switch-handle-width="80px" bs-switch type="checkbox" ng-model="result.pass" switch-size="small" switch-on-text="通过" switch-off-text="驳回"
						 name="result.pass" />
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>